package com.goodwill.androidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TimeUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.pickerview.view.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.goodwill.androidtest.adapter.TestRvAdapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private TextView tv_sub_message;
    private RecyclerView mRecyclerView;
    private Activity mainActivity;
    private TestRvAdapter mTestRvAdapter;

    List<Demo> list = new ArrayList<>();
    private TimePickerView mTimePicker;
    private EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainActivity = this;

        mEditText = findViewById(R.id.editText);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i(TAG, "beforeTextChanged: " + s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i(TAG, "onTextChanged: " + s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "afterTextChanged: Editable -> "+s.toString());
                Log.i(TAG, "afterTextChanged: EditText -> "+s.toString());
            }
        });

//        tv_sub_message = findViewById(R.id.tv_sub_message);

//        String tex = "123465465|qwe";
//        String num = "123465465";
//        boolean isTxtContains = tex.contains("|");
//        boolean isNumContains = num.contains("|");
//        tv_sub_message.setText("tex: " + isTxtContains + "; num: " + isNumContains);

//        tv_sub_message.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(mainActivity, LR105Activity.class));
//            }
//        });

//        tv_sub_message.setOnClickListener(v -> {
//
//        });

//        setRecyclerView();


//        String subMessage = getSubMessage("此医嘱计划执行时间为", "2021-01-18 17:30", ",距执行时间还有", "2", "小时以上！是否继续执行？");
//        String subMessage = String.format("<p><font size=\"3\" color=\"red\">\\%s</font></p>", "guoxiaojun");
//        tv_sub_message.setText(Html.fromHtml("<p><font size=\"3\" color=\"red\">郭小军</font></p>"));    //测试通过
//        tv_sub_message.setText(Html.fromHtml(subMessage));

//        String scannerData = "2000002588719";
//        if (!TextUtils.isEmpty(scannerData) && scannerData.length() == 13) {
//            scannerData = scannerData.substring(0, scannerData.length()-1);
//        }

//        String scannerData = "2000002588719";
//
//        //TIP: 静配中心条码: 条码长度是13位, 最后一位是随机校验码, 扫码时取前12位
//        if (!TextUtils.isEmpty(scannerData) && scannerData.length() == 13) {
//            scannerData = scannerData.substring(0, scannerData.length() - 1);
//        }
//        Log.i("scanner", "onCreate: scannerData---> " + scannerData);

//        Long result = (Long) AviatorEvaluator.execute("1+2+3");
//        Boolean result2 = (Boolean) AviatorEvaluator.execute("2>3");
//        Log.i(TAG, "onCreate: " + result2);

//        String expression = String.format("%d >= 100 && %2d < 130", 120, 120);
//        String expression = String.format("%d >= 100 && %2d < 130", 120, 120);
//        Boolean result = (Boolean) AviatorEvaluator.execute(expression);
////        Expression compiledExp = AviatorEvaluator.compile(expression);
////        Map<String, Object> env = new HashMap<>();
////        env.put("a", 100.3);
////        env.put("b", 45);
////        env.put("c", -199.100);
////        Boolean result = (Boolean) compiledExp.execute(env);
//        Log.i(TAG, "onCreate: " + result);

        //VTE检测逻辑表达式
//        initTest();

//        initTest2();

    }

    private void setRecyclerView() {
        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mTestRvAdapter = new TestRvAdapter(this);
        mRecyclerView.setAdapter(mTestRvAdapter);
        mTestRvAdapter.setNewData(getTestList());
        mTestRvAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

            }
        });
        mTestRvAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_time_content) {
                    Demo item = (Demo) adapter.getItem(position);
                    mTimePicker = CustomTimePicker.initTimePicker(mainActivity, new CustomTimePicker.OnTimeSelectCallback() {
                        @Override
                        public void onTimeSelect(Date date, View view) {
                            if (mTimePicker != null) {
                                mTimePicker.dismiss();
                            }

                            if (date != null) {
                                item.setValue(date.getYear() + "-" + date.getMonth() + 1 + "-" + date.getDay());
                            }
                            adapter.notifyItemChanged(position, item);
                        }
                    });
                    mTimePicker.show();
                }
            }
        });
    }

    private List<Demo> getTestList() {
        list.clear();
        for (int i = 0; i < 50; i++) {
            String title = "数据项" + i + 1;
            list.add(new Demo(ModuleItemType.TIME, title, ""));
        }
        return list;
    }

//    private void initTest2() {
//        //定义表达式
//        String expression = "Hello World  + 用户名";
//        //给表达式中的变量“用户名”付上下文的值
//        List<Variable> variables = new ArrayList<Variable>();
//        variables.add(Variable.createVariable("用户名", "guoxiaojun"));
//        //执行表达式
//        Object result = ExpressionEvaluator.evaluate(expression, variables);
//        System.out.println("Result = " + result);
//    }

    private String getSubMessage(String firstHint, String orderScheduleExecuteTime, String secondHint, String time, String thirdHint) {
        String HTML_TEXT =
                "<p>"
                        + "<font size=\"15\" color=\"black\">" + firstHint + "</font>"
                        + "<font size=\"15\" color=\"red\">" + orderScheduleExecuteTime + "</font>"
                        + "<font size=\"15\" color=\"black\">" + secondHint + "</font>"
                        + "<font size=\"15\" color=\"red\">" + time + "</font>"
                        + "<font size=\"15\" color=\"black\">" + thirdHint + "</font>" +
                        "</p>";

        return HTML_TEXT;
    }

//    private void initTest() {
////        Caprini评分:
////        低危: value>=1 && value<=2
////        中危: value>=3 && value<=4
////        高危: value>=5
//        String lowExp = "value>=1 && value<=2";
//        String middleExp = "value>=3 && value <=4";
//        String heigExp = "value >= 5";
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp, 2));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp, 3));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 2));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 3));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 4));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 5));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp, 6));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 2));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 4));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 5));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 6));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 7));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp, 8));
//
////        Padua评分
////        低危: value>=1 && value<=3
////        中危: value>=4 && value<=6
////        高危: value>=7 && value<=10
//
//        String lowExp2 = "value>=1&&value<=3";
//        String middleExp2 = "value>=4&&value<=6";
//        String heigExp2 = "value>=7&&value<=10";
//
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp2, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp2, 2));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp2, 3));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(lowExp2, 4));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 4));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 5));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 6));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 7));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(middleExp2, 8));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 1));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 2));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 4));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 5));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 6));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 7));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 8));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 10));
//        Log.i(TAG, "initTest: " + isCurrentRiskLevel(heigExp2, 11));
//
//    }
//
//    private Boolean isCurrentRiskLevel(String expression, int score) {
//        Boolean result = false;
//        try {
////            Expression compiledExp = AviatorEvaluator.compile(expression, true);
//            Map<String, Object> env = new HashMap<>();
//            env.put("value", score);
////            result = (Boolean) compiledExp.execute(expression, env);
//            result = (Boolean) AviatorEvaluator.execute(expression, env);
//        } catch (Exception e) {
//            Log.i(TAG, "isCurrentRiskLevel: " + e.getMessage());
//        }
//
//        return result;
//    }

}
